﻿using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;

namespace MyTinnyApi.model
{
    [Table("t_env")]
    public class Env : INotifyPropertyChanged
    {
        private int _id;
        private string _envName;
        private int _showOrder;
        private List<EnvDetail> _values;

        public int Id
        {
            get { return _id; }
            set
            {
                _id = value;
                OnPropertyChanged(nameof(Id));
            }
        }

        public string EnvName
        {
            get { return _envName; }
            set
            {
                _envName = value;
                OnPropertyChanged(nameof(EnvName));
            }
        }

        public int ShowOrder
        {
            get { return _showOrder; }
            set
            {
                _showOrder = value;
                OnPropertyChanged(nameof(ShowOrder));
            }
        }

        [NotMapped]
        public List<EnvDetail> Values
        {
            get
            {
                return _values;
            }
            set
            {
                _values = value;
                _values.ForEach(v =>
                {
                    v.PropertyChanged += V_PropertyChanged;
                });
                OnPropertyChanged(nameof(Values));
            }
        }

        private void V_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            OnPropertyChanged(nameof(Values));
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    [Table("t_env_detail")]
    public class EnvDetail : INotifyPropertyChanged
    {
        private int _id;
        public int Id
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
                OnPropertyChanged(nameof(Id));
            }
        }

        private int _envId;
        public int EnvId
        {
            get
            {
                return _envId;
            }
            set
            {
                _envId = value;
                OnPropertyChanged(nameof(EnvId));
            }
        }

        private string _envCode;
        public string EnvCode
        {
            get
            {
                return _envCode;
            }
            set
            {
                _envCode = value;
                OnPropertyChanged(nameof(EnvCode));
            }
        }

        private string _envValue;
        public string EnvValue
        {
            get
            {
                return _envValue;
            }
            set
            {
                _envValue = value;
                OnPropertyChanged(nameof(EnvValue));
            }
        }

        private int? _showOrder;
        public int? ShowOrder
        {
            get
            {
                return _showOrder;
            }
            set
            {
                _showOrder = value;
                OnPropertyChanged(nameof(ShowOrder));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}

